package com.example.springbootstudy.controller.impl;

import com.example.springbootstudy.controller.DeptController;
import com.example.springbootstudy.pojo.Dept;
import com.example.springbootstudy.pojo.Manager;
import com.example.springbootstudy.pojo.Result;
import com.example.springbootstudy.service.impl.DeptServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//@Controller
//@ResponseBody
@RestController//这一个等同于上面的两个
public class DeptControllerImpl implements DeptController {


    @Autowired
    DeptServiceImpl deptServiceImpl;//自动装配：自动注入DeptServiceImpl实例
//2025/4/1
    /**
     *1.测试
     * @return
     */
    @GetMapping("/addinfo")
    @Override
    public String addController() {
        String s = deptServiceImpl.addService("用户传来需求了");
        return s;
    }

    /**
     * 2.查询
     * @return
     */
    @GetMapping("/selectAll")
    @Override
    public Result selectAllController() {
        return Result.success(deptServiceImpl.selectAllService());
    }

//2025/4/2

    /**
     * 3.删除，在postman中测试
     * @param id
     * @return
     * DeleteMapping("/dept/{id}")此注解表明该方法用于处理 HTTP 的 DELETE 请求，
     * 路径为/dept/{id}，其中{id}属于路径变量。
     */
    @DeleteMapping("/dept/{id}")
    @Override
    public Result delOneController(@PathVariable Integer id) {//把路径变量id绑定到方法参数id上:把路径变量绑定到方法参数上，就是把路径变量动态获取的值传给方法的参数
        deptServiceImpl.delOneService(id);
        return Result.success();
    }

    /**
     * 4.插入
     * @param
     * @return
     */

    @PostMapping("/insertInfo")//如果有getMapping的名字和他相同是不影响的
    @Override
    public Result insertController(@RequestBody  Dept dept) {//将Json转化为Dept
        deptServiceImpl.insertService(dept);
        return Result.success();
    }
    @GetMapping("/getOne/{id}")
    @Override
    public Result getOneController(@PathVariable Integer id) {
        Dept dept=deptServiceImpl.getOneService(id);
        return Result.success(dept);
    }

    @PutMapping("/updateDept")
    @Override
    public Result updateController(@RequestBody Dept dept) {
        deptServiceImpl.updateService(dept);
        return Result.success();
    }
    @PostMapping("/isAdmin")
    @Override
    public Result adminController(@RequestBody Manager manager) {
        Manager manager1 = deptServiceImpl.adminService(manager);
        return Result.success(manager1);
    }
}
